-- UIAvalonCannonMain
-- Created by dengc Sep/20/2016
-- 阿瓦隆巨炮主界面

UIAvalonCannonMain = class("UIAvalonCannonMain", function()
    return cc.Layer:create();
end);

function UIAvalonCannonMain.create(classId, pos)
    return UIAvalonCannonMain.new(classId, pos);
end

-- 构造函数
function UIAvalonCannonMain:ctor(classId, pos)
    self:setName("UIAvalonCannonMain");
    local node = cc.CSLoader:createNode("layout/avalon_cannon/AvalonCannonMain.csb");
    self:addChild(node);
    self.node = node;

    self.BT = findChildByName(self.node, "BT");
    self.CT = findChildByName(self.node, "CT");

    self.classId = classId;
    self.pos = pos;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UIAvalonCannonMain:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIAvalonCannonMain 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIAvalonCannonMain:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.BT, "btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAvalonCannonMain");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.CT, "btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            -- 结果检查
            local result = AvalonCannonM.canAttack();
            if result == "in_cd" then
                alert(getLocStr("avalon_cannon_in_cd"));
                return;
            elseif result == "lack_of_material" then
                alert(getLocStr("attack_lack_of_material"));
                return;
            elseif not result then
                alert(getLocStr("avalon_cannon_cannot_attack"));
                return;
            end

            DungeonActionM.go("avalon_cannon_attack", self.pos);

            alert(getLocStr("avalon_cannon_attack"));

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAvalonCannonMain");
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("avalon_cannon_attack_title"));

    -- 注册跳转按钮点击事件
    local btnTurn = findChildByName(self.BT, "btn_turn");
    local function onTurnClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            -- 打开工程学手册
            local uiForm = UIEngineeringManual.create();
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAvalonCannonMain");
        end
    end
    btnTurn:addTouchEventListener(onTurnClick);
    TextStyleM.setTextStyle(btnTurn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnTurn:setTitleText(getLocStr("engineering_manual_title_1"));
end

-- 重绘
function UIAvalonCannonMain:redraw()
    -- 标题
    local titleLabel = findChildByName(self.CT, "title_node/title");
    local kerning = 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -1;
    end

    titleLabel:setString(getLocStr("avalon_cannon_title"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 设置图片
    local iconImg = findChildByName(self.CT, "icon");
    iconImg:loadTexture(getLevelItemIconPath("avalon_cannon"));

    -- 设置描述
    local descLabel = findChildByName(self.CT, "lb_desc");

    -- 简描述
    local desc = getLocStr("avalon_cannon_desc");

    -- 信息描述
    local avalonCannon = ME.user.avalon_cannon;
    local cannonInfo = string.format(getLocStr("avalon_cannon_info"),
        avalonCannon:getAttack(), avalonCannon:getAccuracy(), avalonCannon:getDurability(), avalonCannon:getMaxDurability());
    desc = spliceLocStr(desc, cannonInfo);

    -- 冷却描述
    local cdInfo = "";
    local cd = avalonCannon:getCountDown();
    if avalonCannon:queryIsLoad() == 1 then
        cdInfo = getLocStr("avalon_cannon_load");
    elseif cd > 0 then
        cdInfo = string.format(getLocStr("avalon_cannon_count_down"), cd);
    else
        local cost = AvalonCannonM.query(self.classId, "attack_cost");
        for key, value in pairs(cost) do
            cdInfo = spliceLocStr(cdInfo, string.format(getLocStr("avalon_cannon_cost"), value, ItemM.query(key, "name")));
        end
    end
    desc = spliceLocStr(desc, cdInfo);

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning);
    descLabel = applyString(descLabel, desc);

    -- 自适应
    local bg = findChildByName(self.node, "CT/bg1");
    local titleNode = findChildByName(self.node, "CT/title_node");
    local btnOk = findChildByName(self.CT, "btn_ok");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_COMMON, {titleNode, descLabel, iconImg, btnOk}, {25, 30, 10}, 40, nil);

    -- 重绘跳转按钮
    self:redrawBtnTurn();
end

-- 重绘跳转按钮
function UIAvalonCannonMain:redrawBtnTurn()
    local btnTurn = findChildByName(self.BT, "btn_turn");

    -- 拥有工程学手册才显示跳转按钮
    if EngineeringManualM.isHaveEngineeringManual() then
        btnTurn:setVisible(true);
    else
        btnTurn:setVisible(false);
    end
end

-- 适配
function UIAvalonCannonMain:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end
